package com.lsy.on_line.zaixian.service.impl;

import com.lsy.on_line.zaixian.model.entity.User;
import com.lsy.on_line.zaixian.mapper.UserMapper;
import com.lsy.on_line.zaixian.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsy.on_line.zaixian.utils.CommonUtils;
import com.lsy.on_line.zaixian.utils.JWTUtils;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ShiYuan
 * @since 2022-07-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 注册：新增用户信息
     * @param userInfo
     * @return
     */
    @Override
    public int save(Map<String, String> userInfo) {
        User user = parseToUser(userInfo);
        if(user != null){
           return userMapper.save(user);
        }else{
        return -1;
        }
    }

    /**
     * 登录
     * @param phone
     * @param pwd
     * @return
     */
    @Override
    public String findByPhoneAndPwd(String phone, String pwd) {
        User user = userMapper.findByPhoneAndPwd(phone,CommonUtils.MD5(pwd));

        if (user == null){
            return null;
        }else {
            String token = JWTUtils.geneJsonWebToken(user);
            return token;
        }
    }

    /**
     * 根据id查询用户信息
     * @param userId
     * @return
     */
    @Override
    public User findByToken(Integer userId) {

        User user = userMapper.findByToken(userId);
        //user.setPwd("");
        return user;
    }

    /**
     * 将map解析为user对象+密码加密处理
     * @param userInfo
     * @return
     */
    private User parseToUser(Map<String, String> userInfo) {
        if(userInfo.containsKey("phone") && userInfo.containsKey("pwd") && userInfo.containsKey("name")){
            User user = new User();
            user.setName(userInfo.get("name"));
            String pwd = userInfo.get("pwd");
            user.setHeadImg(getRandomImg());
            //密码需要MD5加密
            user.setPwd(CommonUtils.MD5(pwd));
            user.setPhone(userInfo.get("phone"));
            user.setCreateTime(new Date());
            return user;
        }else {
            return null;
        }
    }

    /**
     * 随机生成头像
     */
    private static final String [] headImg = {
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cnbeijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    /**
     * 获取随机头像
     * @return
     */
    public String getRandomImg(){
        int size = headImg.length;
        Random random = new Random();
        int index = random.nextInt(size);
        return headImg[index];
    }
}
